package gui;


import javax.swing.*;

import test.Main;
import database.CustomerOperations;
import database.DailyOrderOperations;
import database.OrderItemOperations;
import database.OrderOperations;
import database.ProductOperations;
import model.Customer;
import model.OrderItem;
import model.Orders;
import model.Product;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

@SuppressWarnings("serial")
public class productInputScreen extends JFrame implements ActionListener,ItemListener 
{
	private JPanel p1, p2, p3, p3a,p4, p15;
	private JLabel l1, l2, l3, l15;
	private JButton cancel, ok, add ,remove,clear;
	private JTextField l4;
	private JTextArea area;
	private JComboBox<String>days, product;
	private String dayPicked;
	private String prodPicked;
	private ArrayList<Product> prods;
	private Customer cust;
	private ProductOperations po;
	private String list;
	private ArrayList<String> lists;
	private CustomerOperations co;
	private OrderOperations oo;
	private ArrayList<OrderItem> ordI;
	private DailyOrderOperations doo;
	private OrderItemOperations oio;
	
	public productInputScreen(Customer c)
	{
		super("Product Input Screen");
		this.setSize(840, 500);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLayout(new FlowLayout());
		this.setLocationRelativeTo(null);
		this.cust=c;
		po=new ProductOperations();
		lists=new ArrayList<String>();
		co=new CustomerOperations();
		oo=new OrderOperations();
		ordI=new ArrayList<OrderItem>();
		
		//Panel 1 
		p1 = new JPanel();
		p1.setPreferredSize(new Dimension(800, 70));
		l1 = new JLabel("Choose a delivery day and input the products for that day, press OK when finished or CANCEL to exit.");
		p1.add(l1);
		
		//Panel 1.5 
		p15 = new JPanel();
		p15.setPreferredSize(new Dimension(800, 70));
		l15 = new JLabel("Choose Delivery day: ");
		
		//DAys combo box
		String[] days2={"Monday","Tuesday","Wednsday","Thursday","Friday","Saturday","Sunday"};
		days = new JComboBox<String>(days2);
		days.setPreferredSize(new Dimension(100,25));
		dayPicked=(String)days.getSelectedItem();
		days.addItemListener(this);
		
		p15.add(l15);
		p15.add(days);
		p1.add(p15);
		
		//Panel 2
		p2 = new JPanel();
		area = new JTextArea();
		JScrollPane sp = new JScrollPane(area);
		sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		sp.setPreferredSize(new Dimension (470, 330));
		area.setText("DAY\tNAME\t\t\tQUANTITY\n\n");
		p2.add(sp);
		
		//Panel 3
		
		p3 = new JPanel(new FlowLayout());
		p3.setPreferredSize(new Dimension(330, 230));
		l2 = new JLabel("Choose Product:");
		l3 = new JLabel("Choose Quantity of Product:");
		
		//product combo box filled with names of products
		prods=po.getProductList();
		String[] list =new String[prods.size()];
		for(int i=0;i<prods.size();i++)
		{
			list[i]=prods.get(i).getName() + "  " + prods.get(i).getSupplier();
		}
		product = new JComboBox<String>(list);
		product.setPreferredSize(new Dimension(200,25));
		prodPicked=(String)product.getSelectedItem();
		product.addItemListener(this);
		
		l4=new JTextField();
		l4.setPreferredSize(new Dimension(100,25));
		
		p3a = new JPanel(new GridLayout(1,2));
		p3a.setPreferredSize(new Dimension(300, 25));
		add = new JButton("ADD");
		add.addActionListener(this);
		remove = new JButton("REMOVE");
		remove.addActionListener(this);
		clear = new JButton("CLEAR");
		clear.addActionListener(this);
		
		p3.add(l2);
		p3.add(product);
		p3.add(Box.createRigidArea(new Dimension(300,50)));
		p3.add(l3);
		p3.add(l4);
		p3.add(Box.createRigidArea(new Dimension(300,50)));
		p3.add(p3a);
		
		p3a.add(add ,BorderLayout.WEST);
		p3a.add(remove, BorderLayout.WEST);
		p3a.add(clear, BorderLayout.WEST);
		
		//p4
		p4 = new JPanel();
		p4.setPreferredSize(new Dimension(800, 40));
		
		cancel = new JButton("CANCEL");
		cancel.addActionListener(this);
		p3.add(Box.createRigidArea(new Dimension(15,0)));
		ok = new JButton("OK");
		ok.addActionListener(this);
		
		p4.add(cancel);
		p4.add(Box.createRigidArea(new Dimension(650,0)));
		p4.add(ok);
		
		
		//Adding Panels to the page
		
	this.add(p1, BorderLayout.PAGE_START);
	this.add(p2, BorderLayout.WEST);
	this.add(p3, BorderLayout.EAST);
	this.add(p4, BorderLayout.SOUTH);
	
	
	}
	
	//add a product to screen and array
	public void addProduct()
	{
		if(l4.getText().equals(""))
		{
			//do nothing
		}
		else
		{
			String[] prodOnly=prodPicked.split(" ");
			String prodOnly2="";
			for(int i=0;i<(prodOnly.length)-2;i++)
			{
				prodOnly2+=prodOnly[i]+" ";
			}
			String prodOnly3=prodOnly2.trim();
			boolean well=false;
			for(int i=0;i<ordI.size();i++)
			{
				if(ordI.get(i).getDay()==getDay(dayPicked) && ordI.get(i).getProd()==po.getProdId(prodOnly3))
				{
					well=true;
				}
			}
			if(well==false)
			{
				list=dayPicked + "\t" + prodPicked + "\t\t" + l4.getText() + "\n";
				lists.add(list);
				area.setText("DAY\tNAME\t\t\tQUANTITY\n\n");
				for(int i=0;i<lists.size();i++)
				{
					area.append(lists.get(i));
				}
				ordI.add(new OrderItem(getDay(dayPicked),po.getProdId(prodOnly3) ,Integer.parseInt(l4.getText())));
				l4.setText("");
			}
			else
			{
				JOptionPane.showMessageDialog(null,"You have already choosen a quantity of this product for "+dayPicked,"Illegal Operation",JOptionPane.WARNING_MESSAGE);
				l4.setText("");
			}
		}
		
	}
	
	//remove a product off screen and array
	public void removeProduct()
	{
		if((lists.size()-1)>=0)
		{
			lists.remove(lists.size()-1);
			ordI.remove(ordI.size()-1);
			area.setText("DAY\tNAME\t\t\tQUANTITY\n\n");
			for(int i=0;i<lists.size();i++)
			{
				area.append(lists.get(i));
			}
		}
	}
	
	//get day id
	public int getDay(String n)
	{
		int d=0;
		if(n.equals("Monday"))
		{
			d=1;
		}
		else if(n.equals("Tuesday"))
		{
			d=2;
		}
		else if(n.equals("Wednsday"))
		{
			d=3;
		}
		else if(n.equals("Thursday"))
		{
			d=4;
		}
		else if(n.equals("Friday"))
		{
			d=5;
		}
		else if(n.equals("Saturday"))
		{
			d=6;
		}
		else
		{
			d=7;
		}
		return d;
	}
	
	public void actionPerformed(ActionEvent ae) {
		if(ae.getSource()==add)
		{
			addProduct();
		}
		if(ae.getSource()==remove)
		{
			removeProduct();
		}

		if(ae.getSource()==cancel)
		{
			this.setVisible(false);	
		}
		if(ae.getSource()==ok)
		{
			co.addCustomer(cust);
			System.out.println("customer added");
			int val=co.getCurrentVal();
			System.out.println("cust id "+val);
			System.out.println("user id "+Main.userId);
			Orders o=new Orders(val,Main.userId);
			oo.addOrder(o);
			System.out.println("order added");
			
			co.closeDB2();
			
			int onum=oo.getCurrentVal();
			System.out.println("ord num "+onum);
			doo=new DailyOrderOperations();
			boolean y;
			for(int i=1;i<8;i++)
			{
				y=false;
				for(int j=0;j<ordI.size();j++)
				{
					if(ordI.get(j).getDay()==i)
					{
						y=true;
					}
				}
				if(y==true)
				{
					doo.addDay(onum,i,"Y");
				}
			}
			doo.closeDB3();
			System.out.println("days added");
			
			oio=new OrderItemOperations();
			for(int k=0;k<ordI.size();k++)
			{
				oio.addOrderItem(onum,ordI.get(k).getDay(),ordI.get(k).getProd(),ordI.get(k).getQty());
			}
			oio.closeDB3();
			System.out.println("order items added");
			this.dispose();
		}
		if(ae.getSource()==clear)
		{
			area.setText("DAY\tNAME\t\t\tQUANTITY\n\n");
		}
	}
	
	
	

	public static void main(String args[]) {
		Customer c=null;
		productInputScreen is = new productInputScreen(c);
		is.setVisible(true);
	}

	@Override
	public void itemStateChanged(ItemEvent ae) 
	{
		if (ae.getSource() == days && ae.getStateChange() == ItemEvent.SELECTED)
		{
			dayPicked=(String)days.getSelectedItem();
		}
		if (ae.getSource() == product && ae.getStateChange() == ItemEvent.SELECTED)
		{
			prodPicked=(String)product.getSelectedItem();
		}
	}

}
